package cn.kmsoft.jssc.service.processor;

import cn.kmsoft.jssc.config.AnalyseConfig;
import cn.kmsoft.jssc.entity.analyse.*;
import cn.kmsoft.jssc.service.AnalyseTaskProcessor;
import cn.kmsoft.common.utils.StringUtil;
import cn.kmsoft.wololo.object.GeoJson;
import cn.kmsoft.wololo.object.query.GeoJsonQuery;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.util.*;

/**
 * 土地利用现状调查的地类图斑-叠加占压分析
 * 
 * @author Administrator
 *
 */
public class DltbAnalyseTaskProcessorEx extends AnalyseTaskProcessor {
	private static final Logger logger = LoggerFactory.getLogger(DltbAnalyseTaskProcessorEx.class);
	private static final String defaultDlhynfStr="2021";//默认还原时间年份

	//变更调查-地类图斑
	private final String analysePoint="dltb";
	private AnalyseTaskParam analyseTaskParam;
	private AnalyseConfig analyseConfig;

	public DltbAnalyseTaskProcessorEx(AnalyseTaskParam params, AnalyseConfig analyseConfig) {
		this.analyseTaskParam = params;
		this.analyseConfig = analyseConfig;	
	}
	
	public AnalyseTaskParam getAnalyseTaskParam() {
		return analyseTaskParam;
	}
	public void setAnalyseTaskParam(AnalyseTaskParam analyseTaskParam) {
		this.analyseTaskParam = analyseTaskParam;
	}

	@Override
	public TaskResult call() throws Exception {
		// TODO Auto-generated method stub
		String threadId = Thread.currentThread().getName();
		
		String tableName = this.analyseTaskParam.getTableName();//数据表名称，关键参数
		String analyseType = this.analyseTaskParam.getAnalystType();//intersect/spatialquery/difference
		String dbKey = this.analyseTaskParam.getDbKey();//数据库唯一标识，例如sde126_dbfgc
		String feaTypePoint = analyseTaskParam.getFeaTypePoint();//py/py2

		AnalyseDb analyseDb = this.analyseConfig.getAnalyseDbByDbkey(dbKey);
		if(analyseDb==null){
			System.out.println();
			String errorMsg = String.format("--->TaskProcessor stop, dbKey: %s, analyseDb为null",dbKey);
			System.out.println(errorMsg);
			logger.warn(errorMsg);

			return null;
		}
		String dbType = analyseDb.getDbType();//数据库类型
		String dbInstance = analyseDb.getDbInstance();//数据库实例名
		String tablespace = analyseDb.getTablespace();//表空间
		String serverId = analyseDb.getServerId();//分析服务标识

		String tableId = dbInstance + "/" + tablespace + "." + tableName;
		//注意，根据数据库类型进行分类处置。
		//若为oracle的sde，则需要添加表空间；
		if("postgresql".equalsIgnoreCase(dbType)){
			//若为postgresql的postgis库，则去掉表空间或者为postgis_33_sample.sde.dltb_2018_500155_d36
			tableId = dbInstance + "/" + dbInstance+"."+ tablespace + "." + tableName;
		}
		String operatorMethod = "intersectpy";//分析方法名
		if(analyseType.equalsIgnoreCase("intersect") && feaTypePoint.equalsIgnoreCase("py")){
			operatorMethod = "intersectpy";
		}else if(analyseType.equalsIgnoreCase("intersect") && feaTypePoint.equalsIgnoreCase("py2")){
			operatorMethod = "intersectpy2";
		}else if(analyseType.equalsIgnoreCase("spatialquery") && feaTypePoint.equalsIgnoreCase("py")){
			operatorMethod = "spatialQuery";
		} else if(analyseType.equalsIgnoreCase("spatialquery") && feaTypePoint.equalsIgnoreCase("py2")){
			operatorMethod = "spatialQuery2";
		} else if(analyseType.equalsIgnoreCase("difference") && feaTypePoint.equalsIgnoreCase("py")){
			operatorMethod = "difference";
		}
		AnalyseServer analyseServer = this.analyseConfig.getAnalyseServerByServerId(serverId);
		String serverUrl = analyseServer.getServerUrl();

		String targetUrl = serverUrl + "/" + operatorMethod + "/" + tableId;
		//String targetUrl = this.analyseConfig.getServerUrl() + "/" + operatorMethod + "/" + tableId;
		//String tableId = this.analyseConfig.getDbInstance() + "/" + this.analyseConfig.getDbTablespace() + "." + tableName;
		//String targetUrl = this.analyseConfig.getAnalystServer() + "/" + analystType + "/" + tableId;

		//任务参数
		AnalyseTaskParam params = this.analyseTaskParam;
		params.setTargetUrl(targetUrl);

		//执行分析
		TaskResult taskResult = this.startAnalyse(operatorMethod,threadId, params);
	
		return taskResult;
	}
	
	/**
	 * 开始分析
	 * TaskParam params  
	 * Return 
	 */
	@Override
	public TaskResult startAnalyse(String operatorMethod,String threadId, AnalyseTaskParam params) {
		TaskResult result = new TaskResult();
		
		String targetUrl = params.getTargetUrl();//url请求地址
		String jobId = params.getJobId();
		String taskId = params.getTaskId();
		String dkbh = params.getDkbh();
		double dkmj = params.getDkmj();
		int dlhyNf = params.getDlhynf();

		//范围图形
		GeoJsonQuery query = (GeoJsonQuery) params.getQuery();
		String tableName= params.getTableName();
		
		result.setThreadId(threadId);
		result.setJobId(jobId);
		//分析点
		//result.setAnalystPoint(this.analysePoint);	  
		result.setAnalystPoint(params.getAnalysePoint());
		result.setProcessorName(params.getProcessName());
		result.setTaskId(taskId);
		result.setDkbh(dkbh);
		result.setTableName(tableName);

		int counter=0;
		do {
			counter++;
			System.out.println(String.format("--->doAnalyse,threadId: %s, jobId: %s, dkbh: %s, targetUrl: %s, loopSize: %d", threadId, jobId, dkbh, targetUrl,counter));
			//核心代码
			//dltb分析详细结果
			List<DltbDetailResult> dltbDetailResultList = this.doDltbAnalyse(jobId, targetUrl, query, dlhyNf);
			//对分析结果列表按地类编码的进行面积汇总
			//2-详细到ghtz三级类
			//0101: area
			Map<String, DltbStatis03Result> bgdc03StatisResultMap = this.summaryDltbStatis03Result(dltbDetailResultList);
			//3-汇总到二级类
			//List<GhtzBgdc> ghtzBgdcResults = this.convert2GhtzBgdcList(projectId, dkbh, bgdc03StatisResultMap);

			System.out.println(String.format("---> threadId: %s, jobId:%s, analysePoint: %s, tableName: %s, nf: %d, dkbh: %s, 叠加地类图斑后获得的三级分类数量： %d", threadId, jobId, this.analysePoint, tableName, dlhyNf, dkbh, bgdc03StatisResultMap.size()));
			//4-三级类到一级类
			DltbSummary01Result bgdcSummary = this.summaryBgdcStatisResult(bgdc03StatisResultMap);

			result.setDetailsListResult(dltbDetailResultList);
			result.setSummary03Result(bgdc03StatisResultMap);
			//result.setSummary03DBEntResult(ghtzBgdcResults);
			result.setSummary02Result(null);
			result.setSummary01Result(bgdcSummary);

			//地类图斑是全覆盖的，肯定会有内容
			int sizeOfDetailDltb = dltbDetailResultList.size();
			if( sizeOfDetailDltb>0 ) {
				result.setbAnalystStatus(true);
			}else {
				result.setbAnalystStatus(false);
			}

		}while(result.isbAnalystStatus()==false && counter<= this.maxAnalystCount);

		if(result.isbAnalystStatus()==false) {
			result.setMessage("dltb分析失败!");
		}

		return result;
	}

	/**
	 * dltb叠加分析
	 *
	 * @param targetUrl  叠加分析的目标Url
	 * @param query  GeoJsonQuery对象
	 * @param sjnf   数据年份
	 * @return
	 */
	public List<DltbDetailResult> doDltbAnalyse(String jobId, String targetUrl, GeoJsonQuery query, int sjnf) {
		//压盖的详细地类图斑
		List<DltbDetailResult> dltbDetailResultList = new ArrayList<DltbDetailResult>();
		
		String queryJsonStr = JSONObject.toJSONString(query);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		
		HttpEntity<String> entity = new HttpEntity<String>(queryJsonStr, headers);

		//核心代码
		//转发分析的请求
		ResponseEntity<JSONObject> exchange = this.exchangeSpatialAnalyst(jobId, targetUrl, entity);
		if(exchange==null){
			return dltbDetailResultList;
		}

		JSONObject dltbObject= exchange.getBody();
		if(dltbObject==null || dltbObject.getJSONArray("result")==null) {
			return dltbDetailResultList;
		}

		String result = dltbObject.getJSONArray("result").getJSONObject(0).toJSONString();
		//System.out.println( "--->dltbObject: "+ ss);
		GeoJson geojson= JSONObject.parseObject(result, GeoJson.class);
		JSONArray features = dltbObject.getJSONArray("result").getJSONObject(0).getJSONArray("features");

		//解析后台spatialBoot的叠加分析结果
		//List<Map<String,String>> allFeaProperties = this.parseFeaProperties(features);
		List<Map<String,String>> allFeaProperties = this.parseFeaGeometryAndProperties(features);

		int feaCount = allFeaProperties.size();	
		for(int i=0; i<feaCount;i++ ) {
			Map<String,String> feaProps = allFeaProperties.get(i);

			//将要素属性的key全部转为小写，否则oracle和postgresql数据库返回的结果会有差异
			Map<String,String> feaPropsKeyLowercCase = new HashMap<>();
			feaProps.forEach((key,value) -> feaPropsKeyLowercCase.put(key.toLowerCase(), value));

			//1-目标图层的字段值
			double tbmj = Double.parseDouble(feaPropsKeyLowercCase.get("tbmj").toString());//图斑面积TBMJ
			double tbdlmj = Double.parseDouble(feaPropsKeyLowercCase.get("tbdlmj").toString());//图斑地类面积TBDLMJ

			//2-追加的3个几何面积
			double tqmj = Double.parseDouble(feaPropsKeyLowercCase.get("tqmj").toString());//椭球面积TQMJ，这个面积好像是增加计算的，好像有问题的？？？
			double area = Math.abs(Double.parseDouble(feaPropsKeyLowercCase.get("area").toString()));//叠加后重叠范围的几何面积AREA
			double fgeoarea = Math.abs(Double.parseDouble(feaPropsKeyLowercCase.get("fgeoarea").toString()));//目标图层（sde库）原图斑几何面积FGEOAREA
			double sgeoarea =  Math.abs(Double.parseDouble(feaPropsKeyLowercCase.get("sgeoarea").toString()));//上传图斑的几何面积SGEOAREA

			double kcxs = 0.0; //扣除系数
			double kcmj = 0.0;//扣除面积
			double kchmj = area;//扣除后剩余净面积
			double tkmj = 0.0;//田坎面积

			if( sjnf >= 2019 ) {
				kcxs = Double.parseDouble( !StringUtil.isNullOrEmpty(feaPropsKeyLowercCase.get("kcxs")) ? feaPropsKeyLowercCase.get("kcxs").toString():"0.0");//KCXS
				double kcmj_field = Double.parseDouble(feaPropsKeyLowercCase.get("kcmj").toString());//KCMJ
				String kcdlbm = feaPropsKeyLowercCase.get("kcdlbm").toString();//扣除地类编码KCDLBM

				kcmj = area * kcxs;//扣除面积
				tkmj = kcmj;//田坎面积
				kchmj = area - kcmj;//扣除后净面积
			} else {
				String kcdlbm = feaPropsKeyLowercCase.get("kcdlbm").toString();//扣除地类编码KCDLBM
				double tkxs = Double.parseDouble( !StringUtil.isNullOrEmpty(feaPropsKeyLowercCase.get("tkxs")) ? feaPropsKeyLowercCase.get("tkxs").toString():"0.0");//田坎系数TKXS
				kcxs = tkxs/100;//田坎系数需要除以100，将数值规整到0-1之间

				double xzdwmj_field = Double.parseDouble( StringUtil.isNullOrEmpty(feaPropsKeyLowercCase.get("xzdwmj").toString()) ? "0":feaPropsKeyLowercCase.get("xzdwmj").toString() );//XZDWMJ
				double lxdwmj_field = Double.parseDouble( StringUtil.isNullOrEmpty(feaPropsKeyLowercCase.get("lxdwmj").toString()) ? "0":feaPropsKeyLowercCase.get("lxdwmj").toString() );//LXDWMJ
				double tkmj_field = Double.parseDouble(  StringUtil.isNullOrEmpty(feaPropsKeyLowercCase.get("tkmj").toString())? "0":feaPropsKeyLowercCase.get("tkmj").toString() );//TKMJ

				//核心代码，按面积占比进行扣除面积计算，这是一种近似计算的零星地物和线性地物的方法
				double areaPercent = area/fgeoarea;//面积比例，
				double tkmj_percent = tkmj_field * areaPercent;
				double xzdwmj_percent = xzdwmj_field * areaPercent;
				double lxdwmj_percent = lxdwmj_field * areaPercent;

				kcmj = tkmj_percent + xzdwmj_percent + lxdwmj_percent;//扣除面积，包括田坎面积、现状地物、零星地物

				//权籍所  2023年8月14日
				// 田坎面积= 图斑面积*扣除系数
				tkmj = area * kcxs;
				//kchmj = area - kcmj;
				kchmj = area - tkmj;
			}

			//3-叠加相交结果的几何图形
			String geomText = feaPropsKeyLowercCase.get("geom").toString();
			
			DltbDetailResult dltbDetail = new DltbDetailResult();
			dltbDetail.setFeaProps(feaProps);
			//1-目标图层的字段值
			dltbDetail.setBsm(feaPropsKeyLowercCase.get("bsm").toString());//BSM
			dltbDetail.setTbbh(feaPropsKeyLowercCase.get("tbbh").toString());//TBBH
			dltbDetail.setBm(feaPropsKeyLowercCase.get("dlbm").toString());//DLBM
			dltbDetail.setMc(feaPropsKeyLowercCase.get("dlmc").toString());//DLMC

			dltbDetail.setTqmj(tqmj);//椭球面积
			dltbDetail.setTbmj(tbmj);//图斑面积，来自于字段
			dltbDetail.setTbdlmj(tbdlmj);//图斑地类面积=图斑面积-扣除面积，也等于：图斑面积- 图斑面积*田坎系数。（这里是否需要扣除线性地物面积、零星地物面积呢？？？）
			dltbDetail.setTkxs(kcxs);//田坎系数
			dltbDetail.setZldwdm(feaPropsKeyLowercCase.get("zldwdm").toString());//ZLDWDM
			dltbDetail.setZldwmc(feaPropsKeyLowercCase.get("zldwmc").toString());//ZLDWMC
			dltbDetail.setQsdwdm(feaPropsKeyLowercCase.get("qsdwdm").toString());//QSDWDM
			dltbDetail.setQsdwmc(feaPropsKeyLowercCase.get("qsdwmc").toString());//QSDWMC
			//2-后台新建的新面积字段
			dltbDetail.setFgeoarea(Math.abs(fgeoarea));//目标图层（sde库）原图斑几何面积
			dltbDetail.setSgeoarea(Math.abs(sgeoarea));//上传图斑几何面积

			dltbDetail.setArea(Math.abs(area));//叠加相交几何面积，核心成果
			dltbDetail.setTkarea(tkmj);//田坎面积，注意这里的田坎面积是地块中隐含的田坎面积
			dltbDetail.setKcarea(kcmj);//扣除面积
			dltbDetail.setKcharea(kchmj);//扣除后净面积

			dltbDetail.setGeomText(geomText);
			
			dltbDetailResultList.add(dltbDetail);
		}
		
		return dltbDetailResultList;
	}
	
	
	
	/**
	 * 变更调查dltb-汇总为三级类
	 * 
	 * @param dltbDetailResultList
	 * @return
	 */
	public Map<String,DltbStatis03Result> summaryDltbStatis03Result(List<DltbDetailResult> dltbDetailResultList) {
		//汇总为三级类统计
		Map<String,DltbStatis03Result> dltbStatis03Results = new HashMap<String,DltbStatis03Result>();
		
		Iterator<DltbDetailResult> detailIter = dltbDetailResultList.iterator();
		while( detailIter.hasNext() ) {
			//地类叠加分析结构
			DltbDetailResult dltbDetail = detailIter.next();
			String dlbm = dltbDetail.getBm();
			String dlmc = dltbDetail.getMc();

			//核心代码
			//地类净面积： 计算叠加面积与原图形面积的比例再乘上tbdlmj
			//double area = Math.abs( dltbDetail.getTbdlmj() * ( dltbDetail.getArea()/dltbDetail.getFgeoarea()) );

			double area = dltbDetail.getArea();//相交图形的几何面积
			double tkArea = dltbDetail.getTkarea();//田坎面积
			double kcArea = dltbDetail.getKcarea();//扣除面积
			double kchArea= dltbDetail.getKcharea();//扣除后净面积

			String zldwdm = dltbDetail.getZldwdm();
			String zldwmc = dltbDetail.getZldwmc();
			
			if( dltbStatis03Results.containsKey(dlbm) ) {
				Double areaOfSum = dltbStatis03Results.get(dlbm).getAnalyseArea();
				Double tkAreaOfSum = dltbStatis03Results.get(dlbm).getTkArea();
				Double kcAreaOfSum = dltbStatis03Results.get(dlbm).getKcArea();
				Double kchAreaOfSum = dltbStatis03Results.get(dlbm).getKchArea();

				//新面积+原面积和
				dltbStatis03Results.get(dlbm).setAnalyseArea( areaOfSum + area);
				dltbStatis03Results.get(dlbm).setTkArea( tkAreaOfSum + tkArea);
				dltbStatis03Results.get(dlbm).setKcArea( kcAreaOfSum + kcArea);
				dltbStatis03Results.get(dlbm).setKchArea( kchAreaOfSum + kchArea);

				dltbStatis03Results.get(dlbm).getZldwdmSet().add(zldwdm);
				dltbStatis03Results.get(dlbm).getZldwmcSet().add(zldwmc);
			}
			else {
				DltbStatis03Result statisResult = new DltbStatis03Result();
				statisResult.setDm(dlbm);
				statisResult.setMc(dlmc);
				statisResult.getZldwdmSet().add(zldwdm);
				statisResult.getZldwmcSet().add(zldwmc);

				statisResult.setAnalyseArea(area);
				statisResult.setTkArea(tkArea);
				statisResult.setKcArea(kcArea);
				statisResult.setKchArea(kchArea);

				dltbStatis03Results.put(dlbm, statisResult);
			}
		}
		
	    return dltbStatis03Results;
	}
	
	
	/**
	 * 变更调查-汇总为三级类为一级二级分类
	 * 
	 * @param dltbStatis03ResultMap
	 * @return
	 */
	public DltbSummary01Result summaryBgdcStatisResult(Map<String,DltbStatis03Result> dltbStatis03ResultMap) {
		DltbSummary01Result summary01Result=new DltbSummary01Result();

		//地类编码
		Set<String> dlbmdmSet = dltbStatis03ResultMap.keySet();
		Set<String> zldwmcsSet = new HashSet<String>();//坐落单位名称集合
		Set<String> zldwdmsSet = new HashSet<String>();
		//三大类
		Double nydAreaOfSum = 0.0;//农用地
		Double jsydAreaOfSum = 0.0;//建设用地
		Double wlydAreaOfSum = 0.0;//未利用地
		//三级分类汇总
		Double sdAreaOfSum = 0.0;//湿地
		Double gdAreaOfSum = 0.0;//耕地
		Double ydAreaOfSum = 0.0;//园地
		Double ldAreaOfSum = 0.0;//林地
		Double cdAreaOfSum = 0.0;//草地
		Double syfwAreaOfSum = 0.0;//商业服务
		Double gkydAreaOfSum = 0.0;//工矿用地
		Double zzydAreaOfSum = 0.0;//住宅用地
		Double ggglAreaOfSum = 0.0;//公共管理
		Double tsydAreaOfSum = 0.0;//特殊用地
		Double jtysAreaOfSum = 0.0;//交通运输
		Double syjslssAreaOfSum = 0.0;//水域及水利设施用地
		Double qttdAreaOfSum = 0.0;//其他土地

		Double tkAreaOfSum = 0.0;//田坎面积

		//水田
		Double stAreaOfSum = 0.0;//水田

		Iterator<String> iter= dlbmdmSet.iterator();
		while(iter.hasNext()) {
			String dltbdm = iter.next();
			zldwmcsSet.addAll(dltbStatis03ResultMap.get(dltbdm).getZldwmcSet());
			zldwdmsSet.addAll(dltbStatis03ResultMap.get(dltbdm).getZldwdmSet());

			//核心代码
			//Double area = dltbStatis03ResultMap.get(dltbdm).getAnalyseArea();//几何面积
			Double area = dltbStatis03ResultMap.get(dltbdm).getKchArea();//净面积
			Double tkArea = dltbStatis03ResultMap.get(dltbdm).getTkArea();//田坎面积

			tkAreaOfSum = tkAreaOfSum + tkArea;//田坎面积累计

			int dlhyNf=this.analyseTaskParam.getDlhynf();
			if( dlhyNf> 2018){
				//三大类
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_nyd_sd().contains(dltbdm)) {
					nydAreaOfSum = nydAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_jsyd_sd().contains(dltbdm)) {
					jsydAreaOfSum = jsydAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_wlyd_sd().contains(dltbdm)) {
					wlydAreaOfSum = wlydAreaOfSum + area;
				}

				//湿地
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_sd_sd().contains(dltbdm)) {
					sdAreaOfSum = sdAreaOfSum + area;
				}
				//耕地
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_gd_sd().contains(dltbdm)) {
					gdAreaOfSum = gdAreaOfSum + area;
				}
				//园地
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_yd_sd().contains(dltbdm)){
					ydAreaOfSum = ydAreaOfSum + area;
				}
				//林地
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_ld_sd().contains(dltbdm)){
					ldAreaOfSum = ldAreaOfSum + area;
				}
				//草地
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_cd_sd().contains(dltbdm)){
					cdAreaOfSum = cdAreaOfSum + area;
				}
				//商业服务
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_sfyd_sd().contains(dltbdm)){
					syfwAreaOfSum = syfwAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_gkccyd_sd().contains(dltbdm)) {
					gkydAreaOfSum = gkydAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_zzyd_sd().contains(dltbdm)) {
					zzydAreaOfSum = zzydAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_ggglyfwyd_sd().contains(dltbdm)){
					ggglAreaOfSum = ggglAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_tsyd_sd().contains(dltbdm)){
					tsydAreaOfSum = tsydAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_jtyd_sd().contains(dltbdm)){
					jtysAreaOfSum = jtysAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_syjslssyd_sd().contains(dltbdm)){
					syjslssAreaOfSum = syjslssAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_qttd_sd().contains(dltbdm)){
					qttdAreaOfSum = qttdAreaOfSum + area;
				}

				//水田
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm2_st_sd().contains(dltbdm)){
					stAreaOfSum = stAreaOfSum + area;
				}
			}else {
				//三大类
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_nyd().contains(dltbdm)) {
					nydAreaOfSum = nydAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_jsyd().contains(dltbdm)) {
					jsydAreaOfSum = jsydAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_wlyd().contains(dltbdm)) {
					wlydAreaOfSum = wlydAreaOfSum + area;
				}

				//耕地
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_gd().contains(dltbdm)) {
					gdAreaOfSum = gdAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_yd().contains(dltbdm)){
					ydAreaOfSum = ydAreaOfSum + area;
				}

				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_ld().contains(dltbdm)){
					ldAreaOfSum = ldAreaOfSum + area;
				}

				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_cd().contains(dltbdm)){
					cdAreaOfSum = cdAreaOfSum + area;
				}
				//商业服务
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_sfyd().contains(dltbdm)){
					syfwAreaOfSum = syfwAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_gkccyd().contains(dltbdm)) {
					gkydAreaOfSum = gkydAreaOfSum + area;
				}
				if (this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_zzyd().contains(dltbdm)) {
					zzydAreaOfSum = zzydAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_ggglyfwyd().contains(dltbdm)){
					ggglAreaOfSum = ggglAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_tsyd().contains(dltbdm)){
					tsydAreaOfSum = tsydAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_jtyd().contains(dltbdm)){
					jtysAreaOfSum = jtysAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_syjslssyd().contains(dltbdm)){
					syjslssAreaOfSum = syjslssAreaOfSum + area;
				}
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm_qttd().contains(dltbdm)) {
					qttdAreaOfSum = qttdAreaOfSum + area;
				}

				//水田
				if(this.analyseConfig.getTdlyfldmConfig().getTdlyxzfldm2_st().contains(dltbdm)){
					stAreaOfSum = stAreaOfSum + area;
				}
			}
		}
		summary01Result.setNyd_mj(nydAreaOfSum);
		summary01Result.setJsyd_mj(jsydAreaOfSum);
		summary01Result.setWlyd_mj(wlydAreaOfSum);

		summary01Result.setSd_mj(sdAreaOfSum);//湿地
		summary01Result.setGd_mj(gdAreaOfSum);//耕地
		summary01Result.setYd_mj(ydAreaOfSum);//园地
		summary01Result.setLd_mj(ldAreaOfSum);//林地
		summary01Result.setCd_mj(cdAreaOfSum);//草地
		summary01Result.setSfyd_mj(syfwAreaOfSum);//商业服务
		summary01Result.setGkyd_mj(gkydAreaOfSum);//工矿用地
		summary01Result.setZzyd_mj(zzydAreaOfSum);//住宅用地
		summary01Result.setGggl_mj(ggglAreaOfSum);//公共管理
		summary01Result.setTsyd_mj(tsydAreaOfSum);//特殊用地
		summary01Result.setJtyd_mj(jtysAreaOfSum);//交通运输
		summary01Result.setSyjslss_mj(syjslssAreaOfSum);//水域及水利设施用地
		summary01Result.setQttd_mj(qttdAreaOfSum);//其他土地
		summary01Result.setTkmj(tkAreaOfSum);//田坎面积

		summary01Result.setSt_mj(stAreaOfSum);//水田

		summary01Result.setBgdc_zldwmcsStr(StringUtils.join(zldwmcsSet,","));
		summary01Result.setBgdc_zldwdmsStr(StringUtils.join(zldwdmsSet,","));
		
		return summary01Result;
	}
	
	
	/**
	 * 将变更调查（dltb）的叠加结果转换为三级类实体对象
	 * 
	 * @param projectId
	 * @param dkbh
	 * @param dltbStatis03Results
	 * @return
	 */
	public List<GhtzBgdc> convert2GhtzBgdcList(String projectId, String dkbh, Map<String,DltbStatis03Result> dltbStatis03Results) {
		List<GhtzBgdc> ghtzBgdcResults= new ArrayList<GhtzBgdc>();
		//地类代码
		Set<String> dlbmSet = dltbStatis03Results.keySet();
		Iterator<String> iter = dlbmSet.iterator();
		while(iter.hasNext()) {
			String dlbm = iter.next();
			DltbStatis03Result dltbStatis = dltbStatis03Results.get(dlbm);
			
			GhtzBgdc ghtzBgdc=new GhtzBgdc();
			ghtzBgdc.setProjectId(projectId);
			ghtzBgdc.setDkbh(dkbh);
			ghtzBgdc.setDlbm(dlbm);
			ghtzBgdc.setDlmc(dltbStatis.getMc());	
			ghtzBgdc.setTbdlmj(dltbStatis.getAnalyseArea());
			
			List<String> zldwdmList = new ArrayList<String>(dltbStatis.getZldwdmSet());
			List<String> zldwmcList = new ArrayList<String>(dltbStatis.getZldwmcSet());
			ghtzBgdc.setZldwdm(StringUtils.join(zldwdmList,","));
			ghtzBgdc.setZldwmc(StringUtils.join(zldwmcList,","));
			
			ghtzBgdcResults.add(ghtzBgdc);
		}
		
		return ghtzBgdcResults;
	}
}
